package com.ruiyi.adrobot.component;

import com.alibaba.fastjson.JSONObject;
import com.ruiyi.adrobot.beans.*;
import com.ruiyi.adrobot.beans.dto.TaskExcuteBaseInfo;
import com.ruiyi.adrobot.beans.socket.SocketRequest;
import com.ruiyi.adrobot.mongo.device.DeviceRepository;
import com.ruiyi.adrobot.mongo.robot.RobotRepository;
import com.ruiyi.adrobot.mongo.taskdetail.TaskDetailRepository;
import com.ruiyi.adrobot.service.accountinfo.AccountInfoService;
import com.ruiyi.adrobot.service.device.DeviceService;
import com.ruiyi.adrobot.service.vpn.VpnService;
import com.ruiyi.adrobot.utils.DateUtil;
import com.ruiyi.adrobot.utils.LoggerUtil;
import com.ruiyi.adrobot.utils.RemoteRequestUtil;
import com.ruiyi.adrobot.web.socket.ChannelInitializerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Created by 51102 on 2017/6/24.
 */
@Component
@EnableScheduling
public class Schedule {
    @Resource(name = "redisTemplate")
    private ValueOperations<String, String> redisOperations;

    /**
     * 动态获取IP云accessTocken
     * 每天中午12点触发一次
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void getIpAccessToken() {
        String tokenUrl = "http://router.ipyun.org/api.php/index/token?username=515633307@qq.com&appid=86841a9147b5e82e699b64886b0325c2";
        // 获取accessToken
        JSONObject jsonObject;
        do {
            jsonObject = RemoteRequestUtil.httpRequest(tokenUrl, "GET", null);
        } while (jsonObject.getInteger("status") == 0);
        redisOperations.set("accessToken", jsonObject.getString("access_token"));
        LoggerUtil.info("当前IP云accessToken为" + redisOperations.get("accessToken"));
    }

}
